/*
	sp_ct_mkg_load_marketing_forecast_details '3Com Corporation', 'MSP2000-CB-A1', 11, 24, 2004
*/

ALTER PROC sp_ct_mkg_load_marketing_forecast_details(

@CUSTOMER VARCHAR(100),
@PRODUCT VARCHAR(100),
@MONTH_FROM INT,
@MONTH_TO INT,
@YEAR INT )

AS
BEGIN
	IF RTRIM(@MONTH_FROM) = -1
	BEGIN
		SET @MONTH_FROM = 0
	END

	IF RTRIM(@MONTH_TO) = -1
	BEGIN
		SET @MONTH_TO = 11
	END

	DECLARE @YEAR_TO INT
	SET @YEAR_TO = @YEAR + @MONTH_TO/12
	SET @MONTH_TO = @MONTH_TO%12

	SELECT MF_ID[ID], CREATED_BY[USER], CUSTOMER, PRODUCT, MF_MONTH[MONTH],
	MONTH_NAME = CASE MF_MONTH
		         WHEN 0  THEN 'Jan ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 1  THEN 'Feb ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 2  THEN 'Mar ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 3  THEN 'Apr ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 4  THEN 'May ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 5  THEN 'Jun ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 6  THEN 'Jul ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 7  THEN 'Aug ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 8  THEN 'Sep ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 9  THEN 'Oct ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 10 THEN 'Nov ' + CONVERT(VARCHAR, MF_YEAR)
		         WHEN 11 THEN 'Dec ' + CONVERT(VARCHAR, MF_YEAR)
		      END,
	MF_YEAR[YEAR], MF_QTY[QUANTITY], MF_UNIT_PRICE[UNIT_PRICE], CONVERT(VARCHAR, CREATED_DT, 101)[CREATED_DATE], CONVERT(VARCHAR, CREATED_DT, 101)[MODIFIED_DATE]
	FROM MARKETING_FORECAST_MASTER
	WHERE CUSTOMER = @CUSTOMER
	AND PRODUCT = @PRODUCT
	AND ( ( MF_MONTH >= @MONTH_FROM AND MF_YEAR = @YEAR ) OR
	      ( MF_MONTH <= @MONTH_TO AND MF_YEAR = @YEAR_TO ) OR
	      ( MF_YEAR < @YEAR_TO AND MF_YEAR > @YEAR ) )

END